$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			isCollapse: true
			,queryIng : false//正在查询
			,queryData:{page:1,rows:10}//查询条件
			,datagrid:[]//数据列表数据
			,totalNum:0//分页控件配置
			,data:[]
			,defaultCheckedKeys:[]
			,addData:{}
			,addRoleData:{}
			,selectedRow:null
			,isEdit:false
			,rules:{}
			,dialogConfig:{
				formLabelWidth:'120px'
				,dialogFormVisible:false
				,dialogRoleFormVisible:false
				,urlDialogVisible:false
				,userSelectDialogVisible:false
			}
			,urlDialogContent:''
			,userSelectDialogUrl:staticURL+'/basic/userInfo/goUserInfo?dialogModel=true'
			,treeData:[]
	        ,defaultProps: {
	            children: 'children',
	            label: 'text'
	        }
		}
		,methods: {
			init:function () {
				var vm = this
				vm.query();
				vm.queryTreeData();
			}
			,queryTreeData:function (){
				var vm = this
				var treeUrl = staticURL+'/basic/resourceInfo/resourceTree'
				$.ajax({url :treeUrl,
					cache : false,
					success : function(json) {
						vm.treeData = json;
					}
				});
			}
			,query:function () {
				var vm = this
				if(vm.queryIng){
					return;
				}
				vm.queryIng = true;
				$.ajax({
					url :staticURL+'/basic/groupInfo/datagrid',
					data : vm.queryData,
					dataType : 'json',
					success : function(json) {
						app.datagrid = json.rows;
						app.totalNum = json.total;
						app.queryIng = false;
						app.selectedRow = null;
					}
				});
			},
			search:function(){
				app.queryData.page=1;
				app.query();
			}
			,addItem:function(){
				app.openDialog();
			}
			,deleteItem:function(row){
				
				app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
							url : staticURL+"/basic/groupInfo/delete?id="+row.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				
			}
			,editItem:function(row){
				app.openDialog(row);
			}
			,handleSizeChange:function(val){
				app.queryData.rows=val;
				app.query();
			}
			,handleCurrentChange:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handlePrevClick:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handleNextClick:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handleTreeCheckChange:function(data){
				app.queryData.parentCode = data.id;
				app.query();
			}
			,resetForm:function(formName) {
				app.$refs[formName].resetFields();
		    }
			,submitForm:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						//验证通过
						$.ajax({
							url : app.saveUrl,
							data : app.addData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.dialogFormVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			}
			,openDialog:function(param){
				if(param){
					app.isEdit = true;
					//这里如下操作，而不是 app.addData = param,是为了实现JSON对象的复制
					//如果app.addData = param， 当closeDialog()时，dialogForm 被reset
					//app.addDatat同时被reset,会导致bug
					app.addData = JSON.parse(JSON.stringify(param)); 
				}else{
					app.isEdit = false;
					app.addData = {orderNum:1};
				}
				//显示dialog
				app.dialogConfig.dialogFormVisible=true;
				//重置form
				if(app.$refs['dialogForm']){
					app.$refs['dialogForm'].resetFields();
				}
			}
			,closeDialog:function(){
				app.dialogConfig.dialogFormVisible=false;
				app.resetForm('dialogForm')
			}
			,openRoleDialog:function(param){
				//这里如下操作，而不是 app.addData = param,是为了实现JSON对象的复制
				//如果app.addData = param， 当closeDialog()时，dialogForm 被reset
				//app.addDatat同时被reset,会导致bug
				app.addRoleData.groupId = param.id; 
				
				//显示dialog
				app.dialogConfig.dialogRoleFormVisible=true;
				//重置form
				if(app.$refs['dialogRoleForm']){
					app.$refs['dialogRoleForm'].resetFields();
				}
				app.$refs.tree.setCheckedKeys([]);
			}
			,closeRoleDialog:function(){
				app.dialogConfig.dialogRoleFormVisible=false;
				app.resetForm('dialogRoleForm')
			}
			,datagridSelect:function(row, event, column){
				app.selectedRow = row;
			}
			,handleGroupUser:function(index, row){
				
				layer.open({
					  type: 2, 
					  title:false,
					  area:['100%','100%'],
					  offset:["0px","0px"],
					  icon:0,
					  closeBtn: 0,
					  content: staticURL+'/basic/groupUser/goGroupUser?groupId='+row.id
				}); 
			}
			,handleGroupPrint:function(index, row){
				window.open(staticURL+'/ureport/preview?_u=file:groupinfo_detail.ureport.xml&id='+row.id);
			}
			,handleGroupRole:function(index, row){
		
				$.ajax({
					url :staticURL+"/basic/groupResource/getGroupResource",
					data : {
						groupId : row.id
					},
					dataType : 'json',
					cache : false,
					async : false,
					success : function(json) {
						if(json.upRsIds){
							app.defaultCheckedKeys = json.upRsIds;
						}
					}
				});
				app.openRoleDialog(row);
			}
			,submitRoleForm:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						app.userResouces();
						//验证通过
						$.ajax({
							url : staticURL+"/basic/groupResource/edit",
							data : app.addRoleData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.dialogRoleFormVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			}
			,userResouces:function(){
				 var checkedNodes = this.$refs.tree.getCheckedNodes(false,true); //获取末级且选中的checkbox原dom对象，返回的类型:Array
				 var array = [];
		         for (var i = 0; i < checkedNodes.length; i++) {
		              array.push(checkedNodes[i].id);
		         }
		         this.addRoleData.rsIds = array.join(',');
			}
			,openUrl:function(url){
				app.urlDialogContent = url;
				app.dialogConfig.urlDialogVisible = true;
			}
			,closeUrlDialog:function(){
				app.dialogConfig.urlDialogVisible = false;
			}
			,openUserDialog:function(){
				app.dialogConfig.userSelectDialogVisible = true;
			}
			,doSelectUser:function(){
				var user = userinfoFrame.window._getSelectedUser();
				if(!user){
					app.$notify({showClose: true,
				          message: '请选择用户',
				          type: 'error',
				          position: 'bottom-right'});
				}
				app.dialogConfig.userSelectDialogVisible = false;
				app.dialogConfig.userSelectDialogVisible = false;
				groupinfoFrame.window._selectUser(user);
			}
		},
		computed:{
			saveUrl:function(){
				if(app.isEdit){
					return staticURL+'/basic/groupInfo/edit'
				}else{
					return staticURL+'/basic/groupInfo/add'
				}
			}
			,tableHeight:function(){
				return document.documentElement.clientHeight*0.7;
			}
		}
		,created: function () {
			this.init();
		},
		updated:function(){
		}
	});
});